﻿using MediatR;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Data;
using THE_WIND_RISES.Common.Domans.BasicInformation.MainPlans;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week2.Mainplan_CRUD
{
    public class AddMainplanExcelHander : IRequestHandler<AddMainplanExcelCommand, Result>
    {
        public AddMainplanExcelHander(IBaseRepository<Mainpalan> mainpalanRepository)
        {
            MainpalanRepository = mainpalanRepository;
        }
        public IBaseRepository<Mainpalan> MainpalanRepository { get; set; }
        public async Task<Result> Handle(AddMainplanExcelCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CommonErrorCodes.Added};

            //创建一个表格
            DataTable dtNpoi = new DataTable();
            //工作表路径
            string flieName = Directory.GetCurrentDirectory() + @"\\wwwroot\\Excel\\"+request.ExcelName+"";
            //定义工作表名称
            string sheetName = "主生产计划表 (1)";
            //判断第一行是否是列名
            bool isColumnName=true;
            //工作簿接口
            IWorkbook workbook;
            //获取文件扩展名
            string fileExt = Path.GetExtension(flieName).ToLower();
            //添加数量
            int AddNum = 0;
            //创建文件流
            using (FileStream fs = new FileStream(flieName, FileMode.Open, FileAccess.Read))
            {
                if (fileExt == ".xlsx")
                {
                    workbook = new XSSFWorkbook(fs);
                }
                else
                {
                    workbook = null;
                }
                //导入成功
                //实例化sheet
                ISheet sheet = null;
                if (sheetName != null && sheetName != "")
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null)
                    {
                        //获取表格
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    //获取表格
                    sheet = workbook.GetSheetAt(0);
                }
                for (int i = sheet.FirstRowNum+1; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    var AddMainplanData = "insert into mainpalans values(@MainplanId,@MainplanNum,@MainplanName,@MainplanContext,@MainplanState,@CreateName,@CreateSection,@CreateTime,@ApprovalState,@IsDelMainplan,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
                    var AddNowNum = MainpalanRepository.Add(AddMainplanData, new
                    {
                        MainplanId = 0,
                        MainplanNum = row.GetCell(1).ToString(),
                        MainplanName = row.GetCell(2).ToString(),
                        MainplanContext = row.GetCell(3).ToString(),
                        MainplanState = (row.GetCell(4).ToString() == "新建"?1:(row.GetCell(4).ToString()== "确认"?2:3)),
                        CreateName = row.GetCell(5).ToString(),
                        CreateSection = row.GetCell(6).ToString(),
                        CreateTime=DateTime.Parse(row.GetCell(7).ToString()),
                        ApprovalState = (row.GetCell(8).ToString()=="禁用"?true:false),
                        IsDelMainplan = (row.GetCell(9).ToString() == "禁用" ? true : false),
                        Handle = row.GetCell(10).ToString(),
                        HandleContext = row.GetCell(11).ToString(),
                        HandleTime = DateTime.Parse(row.GetCell(12).ToString()),
                        Pid = (row.GetCell(13).ToString() == "创建" ? 1 : (row.GetCell(13).ToString() == "审批" ? 2 : (row.GetCell(13).ToString()== "删除"?3:4))),
                        IsDelete = (row.GetCell(14).ToString() == "禁用" ? true : false),
                    });
                    if (AddNowNum > 0)
                    {
                        AddNum++;
                    }
                }
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
        }
    }
}
